﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;

namespace SwissTV.Entities.Social10
{
    public class SocialConfigSection : System.Configuration.ConfigurationSection
    {
        [ConfigurationProperty("socialconfigs")]
        [ConfigurationCollection(typeof(SocialConfigCollection), AddItemName = "socialconfig")]
        public SocialConfigCollection SocialConfigs
        {
            get
            {
                return (SocialConfigCollection)this["socialconfigs"];
            }
        }

        public List<SocialConfig> getSocialConfigs()
        {
            return SocialConfigs.Cast<SocialConfigElement>().Select(p => new SocialConfig(p.Authentication.getAuthentication(), p.Urls.getUrls())).ToList();
        }
    }

    public class SocialConfigCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new SocialConfigElement();
        }
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((SocialConfigElement)element).Authentication;
        }

        public SocialConfigElement this[int index]
        {
            get { return (SocialConfigElement)BaseGet(index); }
        }
        new public SocialConfigElement this[string name]
        {
            get { return (SocialConfigElement)BaseGet(name); }
        }
    }

    public class SocialConfigElement : ConfigurationElement
    {
        [ConfigurationProperty("authentication")]
        public AuthenticationElement Authentication { get { return (AuthenticationElement)this["authentication"]; } }

        [ConfigurationProperty("urls")]
        [ConfigurationCollection(typeof(UrlElement), AddItemName = "url")]
        public UrlCollection Urls { get { return (UrlCollection)this["urls"]; } }
    }

    public class ConfigurationTextElement<T> : ConfigurationElement
    {
        private T _value;
        protected override void DeserializeElement(XmlReader reader,
                                bool serializeCollectionKey)
        {
            _value = (T)reader.ReadElementContentAs(typeof(T), null);
        }
        public T Value
        {
            get { return _value; }
        }
    }

    public class AuthenticationElement : ConfigurationElement
    {
        [ConfigurationProperty("mode", DefaultValue = AuthenticationMode.Get, IsRequired = true)]
        public AuthenticationMode Mode
        {
            get
            {
                return (AuthenticationMode)this["mode"];
            }
            set
            {
                this["mode"] = (AuthenticationMode)value;
            }
        }

        [ConfigurationProperty("url")]
        public ConfigurationTextElement<string> Url
        {
            get { return (ConfigurationTextElement<string>)this["url"]; }
        }

        [ConfigurationProperty("userid")]
        public ConfigurationTextElement<string> UserID
        {
            get { return (ConfigurationTextElement<string>)this["userid"]; }
        }

        [ConfigurationProperty("login")]
        public ConfigurationTextElement<string> Login
        {
            get { return (ConfigurationTextElement<string>)this["login"]; }
        }
        [ConfigurationProperty("password")]
        public ConfigurationTextElement<string> Password
        {
            get { return (ConfigurationTextElement<string>)this["password"]; }
        }

        [ConfigurationProperty("applicationcode")]
        public ConfigurationTextElement<string> ApplicationCode
        {
            get { return (ConfigurationTextElement<string>)this["applicationcode"]; }
        }

        [ConfigurationProperty("key")]
        public ConfigurationTextElement<string> Key
        {
            get { return (ConfigurationTextElement<string>)this["key"]; }
        }

        [ConfigurationProperty("hash")]
        public ConfigurationTextElement<string> Hash
        {
            get { return (ConfigurationTextElement<string>)this["hash"]; }
        }

        public AuthenticationInfo getAuthentication()
        {
            return new AuthenticationInfo(Mode, Url.Value, UserID.Value, Login.Value, Password.Value, ApplicationCode.Value, Key.Value, Hash.Value);
        }

    }
    public class UrlCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new UrlElement();
        }
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((UrlElement)element).Name;
        }

        public UrlElement this[int index]
        {
            get { return (UrlElement)BaseGet(index); }
        }
        new public UrlElement this[string name]
        {
            get { return (UrlElement)BaseGet(name); }
        }

        public List<UrlInfo> getUrls()
        {
            return this.Cast<UrlElement>().Select(p=> new UrlInfo(p.Name,p.Value)).ToList();
        }
    }
    public class UrlElement : ConfigurationElement
    {
        [ConfigurationProperty("name")]
        public String Name
        {
            get { return this["name"].ToString(); }
            set { this["name"] = (string)value; }
        }

        [ConfigurationProperty("value")]
        public String Value
        {
            get { return this["value"].ToString(); }
            set { this["value"] = (string)value; }
        }
    }

    public class SocialConfig
    {
        public SocialConfig()
        {
        }

        public SocialConfig(AuthenticationInfo authentication, List<UrlInfo> urls)
            : this()
        {
            this.Authentication = authentication;
            this.Urls = urls;
        }

        public AuthenticationInfo Authentication { get; set; }

        public List<UrlInfo> Urls { get; set; }
    }

    public class AuthenticationInfo
    {
        public AuthenticationInfo()
        {
        }

        public AuthenticationInfo(AuthenticationMode mode, string url, string userID, string login, string password, string applicationCode, string key, string hash)
            : this()
        {
            this.Mode = mode;
            this.Url = url;
            this.UserID = userID;
            this.Login = login;
            this.Password = password;
            this.ApplicationCode = applicationCode;
            this.Key = key;
            this.Hash = hash;
        }

        public AuthenticationMode Mode { get; set; }

        public string Url { get; set; }

        public string UserID { get; set; }

        public string Login { get; set; }

        public string Password { get; set; }

        public string ApplicationCode { get; set; }

        public string Key { get; set; }

        public string Hash { get; set; }
    }


    public class UrlInfo
    {
        public UrlInfo()
        {
        }

        public UrlInfo(string name, string value)
            : this()
        {
            this.Name = name;
            this.Value = value;
        }


        public string Name { get; set; }

        public string Value { get; set; }
    }
    public enum AuthenticationMode
    {
        Get = 0,
        Post = 1,
        Soap = 2
    }
}

